package com.lemon.smartsafety.ui.activity.fire.item;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.RefreshViewEntity;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.entity.tab.TabEntity;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.adapter.ViewPagerAdapter;
import com.lemon.smartsafety.ui.fragment.fire.FirefightingMeFragment;
import com.lemon.smartsafety.ui.fragment.fire.FirefightingOverallFragment;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.UIHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class FirefightingOverallActivity extends NewBaseActivity {


    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.tl_com)
    CommonTabLayout tlCom;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.button)
    Button button;



    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mTitles = {"所有", "我的"};

    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> list;
    private FirefightingMeFragment firefightingMeFragment;
    private FirefightingOverallFragment firefightingOverallFragment;

    private CompanyInfo mCompanyInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            mCompanyInfo = (CompanyInfo) getIntent().getSerializableExtra(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_firefighting_overall;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("综合情况");
    }

    @Override
    protected void initView() {
        initTab();
    }

    private void initTab() {
        list = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }


        for (int i = 0; i < mTitles.length; i++) {
            switch (i) {
                case 0:
                    bundle = new Bundle();
                    //bundle.putString("tab", "tab" + i);
                    firefightingOverallFragment = new FirefightingOverallFragment();
                    firefightingOverallFragment.setArguments(bundle);
                    list.add(firefightingOverallFragment);
                    break;
                case 1:
                    bundle = new Bundle();
                    //bundle.putString("tab", "tab" + i);
                    firefightingMeFragment = new FirefightingMeFragment();
                    firefightingMeFragment.setArguments(bundle);
                    list.add(firefightingMeFragment);
                    break;
            }
        }

        viewPager.setOffscreenPageLimit(mTitles.length + 1);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentPagerAdapter = new ViewPagerAdapter(fragmentManager, list);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                LogUtils.LOGE("Viewpage选择了:" + String.valueOf(i));
                tlCom.setCurrentTab(i);
                if (i == 1) {
                    EventBus.getDefault().post(new RefreshViewEntity(FirefightingMeFragment.class.getName()));
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        tlCom.setTabData(mTabEntities);
        tlCom.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {
//                if (position == 0) {
//                    mTabs.showMsg(0, new Random().nextInt(100) + 1);
//                }
            }
        });
    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }

    @OnClick({R.id.button})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.button:

                LogUtils.LOGE("select:"+tlCom.getCurrentTab());

                Bundle bundle = new Bundle();
                bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
                if(tlCom.getCurrentTab()==0) {
                    if(!CommonUtil.objIsEmpty(firefightingOverallFragment.synthesizeDetail)){
                        bundle.putSerializable(BundleKey.SUPERVISE_TYPE_CODE, firefightingOverallFragment.synthesizeDetail.getSuperviseCode());
                    }
                }
                bundle.putInt(BundleKey.COMPANY_INFO_TYPE,tlCom.getCurrentTab());
                UIHelper.showActivity(mActivity, bundle, FirefightingManageActivity.class);
                break;
        }
    }


}
